﻿using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using Util;
namespace UtilEditors
{
    public class TransformEditers : InspectorCom<Transform>
    {
        public override void Init(SubsidiaryWindow win, int windownID)
        {
            base.Init(win, windownID);
            rect.width = 115;
            rect.height = 300;
        }
        public override void OnIGUI()
        {
            if (Button("看着"))
            {
                component.byInvokeAll((tf) =>
                {
                    var _sceneView = SceneView.lastActiveSceneView;
                    if (_sceneView)
                    {
                        Undo.RecordObject(tf, "Look At Transform Position");
                        tf.setActive(false);
                        tf.position = _sceneView.pivot.toVector2();
                        tf.setActive(true);
                    }
                });
            }
            if (Button("加一个"))
            {
                component.byInvokeAll((tf) =>
                {
                    var n = (new GameObject("Node_" + debug.Sole));
                    n.transform.parent = tf;
                    n.transform.toResetLocal();
                    Undo.RegisterCreatedObjectUndo(n, "Create object");
                });
            }
            if (Button("重置位置"))
            {
                component.byInvokeAll((tf) =>
                {
                    Undo.RecordObject(tf, "Zero Transform Position");
                    tf.toResetLocal();
                });
            }
            if (Button("去除精度"))
            {
                component.byInvokeAll((tf) =>
                {
                    Undo.RecordObject(tf, "去除精度");
                    tf.localPosition = tf.localPosition.toRounds(2);
                    tf.localEulerAngles = tf.localEulerAngles.toRounds(1);
                    tf.localScale = tf.localScale.toRounds(2);
                });
            }
            if (Button("父节点同步位置"))
            {
                component.byInvokeAll((tf) =>
                {
                    var parent = tf.parent;
                    Undo.RecordObject(parent, "父节点同步位置");
                    var pos = tf.position;
                    var t = new GameObject();
                    t.transform.position = pos;

                    for (int i = parent.childCount - 1; i >= 0; i--)
                    {
                        parent.GetChild(i).parent = t.transform;
                    }
                    parent.position = pos;
                    for (int i = t.transform.childCount - 1; i >= 0; i--)
                    {
                        t.transform.GetChild(i).parent = parent;
                    }
                    GameObject.DestroyImmediate(t);
                    Undo.RecordObject(tf, "父节点同步位置");

                });
            }
            if (SelectionObj.Length >= 2)
            {
                if (Button("组合"))
                {
                    组合();
                }
            }
            if (SelectionObj.Length >= 1)
            {
                if (Button("New父节点"))
                {
                    New父节点();

                }
                if (Button("Del父节点"))
                {
                    Del父节点();
                }
            }
            if (Button("排序"))
            {
                component.Sort((a, b) => { return a.name.CompareTo(b.name); });
                component.byInvokeAll((tf) =>
                {
                    tf.SetAsFirstSibling();
                });
            }
        }
        void 组合()
        {
            var par = SelectionObj[0].transform.parent;
            var n = (new GameObject("Node_" + debug.Sole));
            n.transform.parent = par;
            Undo.RegisterCreatedObjectUndo(n, "Create object");
            n.transform.position = SelectionObj[0].transform.position;
            for (int i = 0; i < SelectionObj.Length; i++)
            {
                Undo.SetTransformParent(SelectionObj[i].transform, n.transform, "Set 父节点");
                SelectionObj[i].transform.parent = n.transform;
            }
        }
        void New父节点()
        {
            for (int i = 0; i < SelectionObj.Length; i++)
            {
                var cur = SelectionObj[i].transform;
                var par = cur.parent;
                var n = (new GameObject(cur.name));
                Undo.RegisterCreatedObjectUndo(n, "Create object");
                n.transform.parent = SelectionObj[i].transform;
                n.transform.toResetLocal();
                Undo.SetTransformParent(n.transform, par, "Set 父节点");
                n.transform.parent = par;
                Undo.SetTransformParent(cur, n.transform, "Set 父节点");
                cur.parent = n.transform;
            }
        }
        void Del父节点()
        {
            List<GameObject> list = new List<GameObject>();
            for (int i = 0; i < SelectionObj.Length; i++)
            {
                var cur = SelectionObj[i].transform;
                var par = cur.parent;
                var parpar = par.parent;
                Undo.SetTransformParent(cur, parpar, "Set 父节点");
                cur.parent = parpar;
                if (list.IndexOf(par.gameObject) < 0)
                {
                    list.Add(par.gameObject);
                }

            }
            for (int i = 0; i < list.Count; i++)
            {
                Undo.DestroyObjectImmediate(list[i].gameObject);
                GameObject.DestroyImmediate(list[i].gameObject);
            }
        }

    }
}